/**
 * Created by ZT on 2018/10/9 0009.
 */

/*获取相册类型*/
const sqls = require('../sqlMap');  //对应的mysql语句
const query = require('../db');

/**    
 *   后 台 相 册
 *   相册里面包含相片
 *      execPhoto    获取所有相册 
 * getPhotoBySort   通过相册获取所有相片
 *      postPhoto   相片添加
 *      savePhoto   保存图片
*/

exports.execPhoto=(req,res,next)=>{
        var params = req.query;
        var sql=sqls.findData('zt_photosort','');
        query.connect(sql,function (err,result) {
            let results={};
            if(err){
                 results = { 'ret': 0, 'msg': '获取失败'};
                res.send(results);
            }else {
                 results = { 'ret': 1, 'rows': result };
                res.send(results);
            }

        });
}
//-----------------------查找相册里面的照片---
exports.getPhotoBySort=(req,res,next)=>{
    let params = req.query;
    console.log(params,333);
    let where = `where sort =${params.id}`;
    let sql = sqls.findData('zt_photo', where);
    query.connect(sql,function (err,result) {
        let results={};
        if(err){
            results = { 'ret': 0, 'msg': '添加失败'};
            res.send(results);
        }else {
            results = { 'ret': 1, 'rows': result};
            res.send(results);
        }
    });
}

////---------------------------相片添加-----
exports.postPhoto=(res,req,next)=>{
    var eachValue = [];
    var feild = 'title,abstr,url,sort';
    eachValue.push("'" + req.body.title + "'"); //标题
    eachValue.push("'" + req.body.abstr + "'");  // 概要
    eachValue.push("'" + req.body.url + "'"); //图片
    eachValue.push("'" + req.body.sort + "'"); //排序
    let _sql = sqls.insertTable('zt_photo', feild, eachValue);
    query.connect(_sql,function (err,result) {
        let results={};
        if(err){
                results = { 'ret': 0, 'msg': '添加失败'};
            res.send(results);
        }else {
            results = { 'ret': 1};
            res.send(results);
        }
    });
}

//000------------------图片存储请求接口
exports.savePhoto = (res,req,next)=>{
    const ueditorConfig = require('../../static/ueditor/ueditor.config.json');//公共方
    let imgDir = '/ueditor/images' //默认上传地址为图片
    let ActionType = res.query.action;
    if (ActionType === 'uploadimage' || ActionType === 'uploadfile' || ActionType === 'uploadvideo') {
        var file_url = imgDir;//默认上传地址为图片
        /*其他上传格式的地址*/
        if (ActionType === 'uploadfile') {
            file_url = '/file/ueditor/'; //附件保存地址
        }
        if (ActionType === 'uploadvideo') {
            file_url = '/video/ueditor/'; //视频保存地址
        }
        req.setHeader('Content-Type', 'application/json');
        req.ue_up(file_url); //你只要输入要保存的地址 。保存操作交给ueditor来做
    }
    //客户端发起图片列表请求
    else if (ActionType === 'listimage') {

        req.ue_list(imgDir);  // 客户端会列出 dir_url 目录下的所有图片
    }
    // 客户端发起其它请求
    else {
        req.jsonp(ueditorConfig);  
        /* res.setHeader('Content-Type', 'application/json');
         res.redirect('http://127.0.0.1:8081/static/ueditor/nodejs/config.json');*/
    }


}